home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Atlanta_1990 / Atlanta-Devcon.2 / Libraries / GadTools / menu1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-26  |  7.9 KB  |  372 lines

  1. /*
  2. ** menu1.c:  Menu example with font-sensitivity.
  3. **
  4. ** (C) Copyright 1990, Commodore-Amiga, Inc.
  5. **
  6. ** Executables based on this information may be used in software
  7. ** for Commodore Amiga computers.  All other rights reserved.
  8. ** This information is provided "as is"; no warranties are made.  All
  9. ** use is at your own risk. No liability or responsibility is assumed.
  10. */
  11.  
  12. #include <exec/types.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/gadtools.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/graphics_protos.h>
  19. #include <clib/intuition_protos.h>
  20. #include <clib/diskfont_protos.h>
  21. #include <clib/gadtools_protos.h>
  22.  
  23. void printf(STRPTR,...);
  24. int stcd_l(char *, long *);
  25. void exit(int);
  26.  
  27. /*------------------------------------------------------------------------*/
  28.  
  29. void main(int, char *[]);
  30. void bail_out(int);
  31. BOOL HandleMenuEvent(UWORD);
  32. BOOL OpenFunc(UWORD);
  33. BOOL SaveFunc(UWORD);
  34. BOOL PrintFunc(UWORD);
  35. BOOL QuitFunc(UWORD);
  36.  
  37. /*------------------------------------------------------------------------*/
  38.  
  39. /*  Here we specify what we want our menus to contain: */
  40.  
  41. struct NewMenu mynewmenu[] =
  42. {
  43.     { NM_TITLE, "Project",      0 , 0, 0, 0,},
  44.     {  NM_ITEM, "Open...",     "O", 0, 0, OpenFunc,},
  45.     {  NM_ITEM, "Save",      0 , 0, 0, SaveFunc,},
  46.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  47.     {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  48.     {   NM_SUB, "Draft",      0 , 0, 0, PrintFunc,},
  49.     {   NM_SUB, "NLQ",      0 , 0, 0, PrintFunc,},
  50.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  51.     {  NM_ITEM, "Quit...",     "Q", 0, 0, QuitFunc,},
  52.  
  53.     { NM_TITLE, "Edit",      0 , 0, 0, 0,},
  54.     {  NM_ITEM, "Cut",     "X", 0, 0, 0,},
  55.     {  NM_ITEM, "Copy",     "C", 0, 0, 0,},
  56.     {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  57.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  58.     {  NM_ITEM, "Undo",     "Z", 0, 0, 0,},
  59.  
  60.     {   NM_END, 0,          0 , 0, 0, 0,},
  61. };
  62.  
  63. /*------------------------------------------------------------------------*/
  64.  
  65. struct TextAttr customtattr;
  66. struct TextAttr *tattr;
  67.  
  68. /*------------------------------------------------------------------------*/
  69.  
  70. extern struct Library *SysBase;
  71. struct GfxBase *GfxBase = NULL;
  72. struct IntuitionBase *IntuitionBase = NULL;
  73. struct Library *GadToolsBase = NULL;
  74. struct Library *DiskfontBase = NULL;
  75. struct Screen *mysc = NULL;
  76. struct Menu *menu = NULL;
  77. struct Window *mywin = NULL;
  78. struct TextFont *customfont = NULL;
  79. void *vi = NULL;
  80.  
  81. /*------------------------------------------------------------------------*/
  82.  
  83. BOOL terminated;
  84.  
  85. /*------------------------------------------------------------------------*/
  86.  
  87. void main(argc, argv)
  88.  
  89. int argc;
  90. char *argv[];
  91.  
  92. {
  93.     struct IntuiMessage *imsg;
  94.     ULONG imsgClass;
  95.     UWORD imsgCode;
  96.  
  97.     terminated = FALSE;
  98.  
  99.     if (argc == 2)
  100.     {
  101.     printf("Usage:\n\tmenu1\nor\n\tmenu1 fontname.font fontsize\n");
  102.     printf("Example:\n\tmenu1 courier.font 15\n");
  103.     bail_out(0);
  104.     }
  105.     /*  Open all libraries: */
  106.  
  107.     if (!(GfxBase = (struct GfxBase *)
  108.     OpenLibrary("graphics.library", 36L)))
  109.     bail_out(20);
  110.  
  111.     if (!(IntuitionBase = (struct IntuitionBase *)
  112.     OpenLibrary("intuition.library", 36L)))
  113.     bail_out(20);
  114.  
  115.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 36L)))
  116.     bail_out(20);
  117.  
  118.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 36L)))
  119.     bail_out(20);
  120.  
  121.     if (!(mysc = LockPubScreen(NULL)))
  122.     bail_out(20);
  123.  
  124.     if (!(vi = GetVisualInfo(mysc,
  125.     TAG_DONE)))
  126.     bail_out(20);
  127.  
  128.     customtattr.ta_Style = 0;
  129.     customtattr.ta_Flags = 0;
  130.     if (argc < 3)
  131.     {
  132.     /*  Default to screen's font */
  133.     tattr = mysc->Font;
  134.     }
  135.     else
  136.     {
  137.     LONG longval;
  138.  
  139.     /*  Attempt to use the font specified on the command line: */
  140.     customtattr.ta_Name = argv[1];
  141.     /*  Convert decimal size to long */
  142.     stcd_l(argv[2], &longval);
  143.     customtattr.ta_YSize = longval;
  144.     tattr = &customtattr;
  145.     if (!(customfont = OpenDiskFont(tattr)))
  146.     {
  147.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  148.         customtattr.ta_YSize);
  149.         bail_out(20);
  150.     }
  151.     }
  152.  
  153.     /*  Build and layout menus using the right font: */
  154.     if (!(menu = CreateMenus(mynewmenu,
  155.     GTMN_FrontPen, 0,
  156.     TAG_DONE)))
  157.     bail_out(20);
  158.  
  159.     if (!LayoutMenus(menu, vi,
  160.     GTMN_TextAttr, tattr,
  161.     TAG_DONE))
  162.     bail_out(20);
  163.  
  164.     if (!(mywin = OpenWindowTags(NULL,
  165.     WA_Width, 400,
  166.     WA_InnerHeight, 100,
  167.  
  168.     WA_Activate, TRUE,
  169.     WA_DragBar, TRUE,
  170.     WA_DepthGadget, TRUE,
  171.     WA_CloseGadget, TRUE,
  172.     WA_SizeGadget, TRUE,
  173.     WA_SimpleRefresh, TRUE,
  174.  
  175.     WA_IDCMP, CLOSEWINDOW | REFRESHWINDOW | MENUPICK,
  176.  
  177.     WA_MinWidth, 50,
  178.     WA_MinHeight, 50,
  179.     WA_Title, "GadTools Menu Demo",
  180.  
  181.     TAG_DONE)))
  182.     bail_out(20);
  183.  
  184.     SetMenuStrip(mywin, menu);
  185.  
  186.     while (!terminated)
  187.     {
  188.     Wait (1 << mywin->UserPort->mp_SigBit);
  189.     /*  GT_GetIMsg() returns a cooked-up IntuiMessage with
  190.         more friendly information for complex gadget classes.  Use
  191.         it wherever you get IntuiMessages: */
  192.     while ((!terminated) && (imsg = GT_GetIMsg(mywin->UserPort)))
  193.     {
  194.         imsgClass = imsg->Class;
  195.         imsgCode = imsg->Code;
  196.         /*  Use the toolkit message-replying function here... */
  197.         GT_ReplyIMsg(imsg);
  198.         switch (imsgClass)
  199.         {
  200.         case MENUPICK:
  201.             terminated = HandleMenuEvent(imsgCode);
  202.             break;
  203.  
  204.         case CLOSEWINDOW:
  205.             printf("CLOSEWINDOW.\n");
  206.             terminated = TRUE;
  207.             break;
  208.  
  209.         case REFRESHWINDOW:
  210.             printf("REFRESHWINDOW.\n");
  211.             /*  You must use GT_BeginRefresh() where you would
  212.             normally have your first BeginRefresh() */
  213.             GT_BeginRefresh(mywin);
  214.             GT_EndRefresh(mywin, TRUE);
  215.             break;
  216.         }
  217.     }
  218.     }
  219.     bail_out(0);
  220. }
  221.  
  222. /*------------------------------------------------------------------------*/
  223.  
  224. /*/ bail_out()
  225.  *
  226.  * Function to close down or free any opened or allocated stuff, and then
  227.  * exit.
  228.  *
  229.  */
  230.  
  231. void bail_out(code)
  232.  
  233. int code;
  234.  
  235. {
  236.     if (mywin)
  237.     {
  238.     ClearMenuStrip(mywin);
  239.     CloseWindow(mywin);
  240.     }
  241.  
  242.     /*  None of these two calls mind a NULL parameter, so it's not
  243.     necessary to check for non-NULL before calling.  If we do that,
  244.     we must be certain that the OpenLibrary() of GadTools succeeded,
  245.     or else we would be jumping into outer space: */
  246.     if (GadToolsBase)
  247.     {
  248.     FreeMenus(menu);
  249.     FreeVisualInfo(vi);
  250.     CloseLibrary(GadToolsBase);
  251.     }
  252.  
  253.     if (customfont)
  254.     {
  255.     CloseFont(customfont);
  256.     }
  257.  
  258.     if (mysc)
  259.     {
  260.     UnlockPubScreen(NULL, mysc);
  261.     }
  262.  
  263.     if (DiskfontBase)
  264.     {
  265.     CloseLibrary(DiskfontBase);
  266.     }
  267.  
  268.     if (IntuitionBase)
  269.     {
  270.     CloseLibrary(IntuitionBase);
  271.     }
  272.  
  273.     if (GfxBase)
  274.     {
  275.     CloseLibrary(GfxBase);
  276.     }
  277.  
  278.     exit(code);
  279. }
  280.  
  281.  
  282. /*------------------------------------------------------------------------*/
  283.  
  284. /*/ HandleMenuEvent()
  285.  *
  286.  * This function handles IntuiMessage events of type MENUPICK.  It
  287.  * demonstrates one of the best uses for the MenuItem UserData field
  288.  * provided by GadTools, namely a place to store pointers-to-functions.
  289.  *
  290.  */
  291.  
  292. BOOL HandleMenuEvent(code)
  293.  
  294. UWORD code;
  295.  
  296. {
  297.     struct MenuItem *item;
  298.     BOOL terminated = FALSE;
  299.     BOOL (*fptr)(UWORD);
  300.  
  301.     printf("MENUPICK:  ");
  302.     /*  Get all menu events including NextEvents until a terminating
  303.     selection is made (such as Quit) */
  304.     while ((code != MENUNULL) && (!terminated))
  305.     {
  306.     item = ItemAddress(menu, code);
  307.     if (fptr = MENU_USERDATA(item))
  308.     {
  309.         terminated = (*fptr)(code);
  310.     }
  311.     else
  312.     {
  313.         printf("No function.  ");
  314.     }
  315.     code = item->NextSelect;
  316.     }
  317.     printf("\n");
  318.  
  319.     return(terminated);
  320. }
  321.  
  322. /*------------------------------------------------------------------------*/
  323.  
  324. BOOL OpenFunc(code)
  325.  
  326. UWORD code;
  327.  
  328. {
  329.     printf("OpenFunc called.  ");
  330.     return(FALSE);
  331. }
  332.  
  333. /*------------------------------------------------------------------------*/
  334.  
  335. BOOL SaveFunc(code)
  336.  
  337. UWORD code;
  338.  
  339. {
  340.     printf("SaveFunc called.  ");
  341.     return(FALSE);
  342. }
  343. /*------------------------------------------------------------------------*/
  344.  
  345. BOOL PrintFunc(code)
  346.  
  347. UWORD code;
  348.  
  349. {
  350.     printf("PrintFunc called ");
  351.     if (code == FULLMENUNUM(0, 3, 0))
  352.     {
  353.     printf("(Draft).  ");
  354.     }
  355.     else
  356.     {
  357.     printf("(NLQ).  ");
  358.     }
  359.     return(FALSE);
  360. }
  361. /*------------------------------------------------------------------------*/
  362.  
  363. BOOL QuitFunc(code)
  364.  
  365. UWORD code;
  366.  
  367. {
  368.     printf("QuitFunc called.  ");
  369.     return(TRUE);
  370. }
  371. /*------------------------------------------------------------------------*/
  372.